package com.ruoyi.kanban.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kanban.domain.DeliveryOrder;
import com.ruoyi.kanban.mapper.DeliveryOrderMapper;
import com.ruoyi.kanban.service.IDeliveryOrderService;

@Service
public class DeliveryOrderServiceImpl extends ServiceImpl<DeliveryOrderMapper, DeliveryOrder>
        implements IDeliveryOrderService {

    @Override
    public List<DeliveryOrder> selectDeliveryOrderList(DeliveryOrder query) {
        LambdaQueryWrapper<DeliveryOrder> w = buildWrapper(query);
        // 严格按当前数据库表结构查询：排除不存在的 remark 字段
        w.select(DeliveryOrder.class, info -> !"remark".equals(info.getColumn()));
        return this.list(w);
    }

    @Override
    public DeliveryOrder selectDeliveryOrderById(Long id) {
        LambdaQueryWrapper<DeliveryOrder> w = new LambdaQueryWrapper<>();
        // 严格按当前数据库表结构查询：排除不存在的 remark 字段
        w.select(DeliveryOrder.class, info -> !"remark".equals(info.getColumn()));
        w.eq(DeliveryOrder::getId, id);
        return this.getOne(w);
    }

    @Override
    public boolean insertDeliveryOrder(DeliveryOrder entity) {
        // 统一设置默认值，避免 NOT NULL 插入错误
        if (entity.getKanbanId() == null) {
            entity.setKanbanId(0L);
        }
        if (entity.getFrameworkId() == null) {
            entity.setFrameworkId(0L);
        }
        if (entity.getDelFlag() == null) {
            entity.setDelFlag(0);
        }
        if (entity.getStatus() == null) {
            entity.setStatus(10);
        }
        return this.save(entity);
    }

    @Override
    public boolean updateDeliveryOrder(DeliveryOrder entity) {
        return this.updateById(entity);
    }

    @Override
    public boolean deleteDeliveryOrderByIds(List<Long> ids) {
        return this.removeByIds(ids);
    }

    private LambdaQueryWrapper<DeliveryOrder> buildWrapper(DeliveryOrder q) {
        LambdaQueryWrapper<DeliveryOrder> w = new LambdaQueryWrapper<>();

        // 单据/关联编码筛选
        if (StringUtils.isNotEmpty(q.getDeliveryCode())) {
            w.like(DeliveryOrder::getDeliveryCode, q.getDeliveryCode());
        }
        if (StringUtils.isNotEmpty(q.getKanbanCode())) {
            w.like(DeliveryOrder::getKanbanCode, q.getKanbanCode());
        }
        if (StringUtils.isNotEmpty(q.getFrameworkCode())) {
            w.like(DeliveryOrder::getFrameworkCode, q.getFrameworkCode());
        }

        // 公司筛选
        if (q.getReceiverCompanyId() != null) {
            w.eq(DeliveryOrder::getReceiverCompanyId, q.getReceiverCompanyId());
        }
        if (StringUtils.isNotEmpty(q.getReceiverCompanyName())) {
            w.like(DeliveryOrder::getReceiverCompanyName, q.getReceiverCompanyName());
        }
        if (q.getSenderCompanyId() != null) {
            w.eq(DeliveryOrder::getSenderCompanyId, q.getSenderCompanyId());
        }
        if (StringUtils.isNotEmpty(q.getSenderCompanyName())) {
            w.like(DeliveryOrder::getSenderCompanyName, q.getSenderCompanyName());
        }

        // 物料筛选
        if (StringUtils.isNotEmpty(q.getMaterialCode())) {
            w.like(DeliveryOrder::getMaterialCode, q.getMaterialCode());
        }
        if (StringUtils.isNotEmpty(q.getMaterialName())) {
            w.like(DeliveryOrder::getMaterialName, q.getMaterialName());
        }

        // 状态/异常筛选
        if (q.getStatus() != null) {
            w.eq(DeliveryOrder::getStatus, q.getStatus());
        }
        if (q.getExceptionFlag() != null) {
            w.eq(DeliveryOrder::getExceptionFlag, q.getExceptionFlag());
        }
        if (q.getExceptionType() != null) {
            w.eq(DeliveryOrder::getExceptionType, q.getExceptionType());
        }

        // 创建时间范围、到货时间范围（从 params 中获取）
        if (q.getParams() != null) {
            Object beginCreateTime = q.getParams().get("beginCreateTime");
            Object endCreateTime = q.getParams().get("endCreateTime");
            if (beginCreateTime != null && StringUtils.isNotEmpty(beginCreateTime.toString())) {
                w.ge(DeliveryOrder::getCreateTime, beginCreateTime.toString() + " 00:00:00");
            }
            if (endCreateTime != null && StringUtils.isNotEmpty(endCreateTime.toString())) {
                w.le(DeliveryOrder::getCreateTime, endCreateTime.toString() + " 23:59:59");
            }

            Object beginRequired = q.getParams().get("beginRequiredDeliveryTime");
            Object endRequired = q.getParams().get("endRequiredDeliveryTime");
            if (beginRequired != null && StringUtils.isNotEmpty(beginRequired.toString())) {
                w.ge(DeliveryOrder::getRequiredDeliveryTime, beginRequired.toString() + " 00:00:00");
            }
            if (endRequired != null && StringUtils.isNotEmpty(endRequired.toString())) {
                w.le(DeliveryOrder::getRequiredDeliveryTime, endRequired.toString() + " 23:59:59");
            }
        }

        // 默认未删除与时间倒序
        w.eq(DeliveryOrder::getDelFlag, 0);
        w.orderByDesc(DeliveryOrder::getCreateTime);
        return w;
    }
}